package com.ijiuchuang.jc_localization.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.bean.api.Keji;
import com.ijiuchuang.jc_localization.network.ApiModel;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.DividerItemDecoration;
import com.ijiuchuang.jc_localization.tools.utils.Log;
import com.ijiuchuang.jc_localization.view.adapter.TrendsListRecyclerAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class TrendsFragment extends Fragment {

    @BindView(R.id.trends_recycler)
    RecyclerView mRecyclerView;

    private TrendsListRecyclerAdapter mRecyclerAdapter;

    public TrendsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_trends, container, false);
        ButterKnife.bind(this, view);

        ApiModel.apiStore()
                .getKeji(10, 1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Keji>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("onError in TrendsFragment: " + e.toString());
                    }

                    @Override
                    public void onNext(Keji keji) {
                        mRecyclerAdapter = new TrendsListRecyclerAdapter(getActivity(), R.layout.recycler_item_keji, keji.getNewslist());
                        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
                        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
                        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
                        mRecyclerView.setAdapter(mRecyclerAdapter);
                    }
                });

        return view;
    }

}
